package cc.wsyw126.java.nowcoder;

/**
 * Created by Administrator on 2017/4/12.
 */
public class P01Method2 {
    int getMax(int a, int b) {
        return a > b ? a : b;
    }

    public int backPackII(int m, int[] A, int V[]) {
        int n = A.length;
        int[][] max = new int[n][m + 1];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= m; ++j) {
                max[i][j] = 0;
            }
        }
        for (int l = 0; l <= m; ++l) {
            if (l < A[0]) {
                max[0][l] = 0;
            } else {
                max[0][l] = V[0];
            }
        }
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (j < A[i]) {
                    max[i][j] = max[i - 1][j];
                } else {
                    max[i][j] = getMax(max[i - 1][j - A[i]] + V[i], max[i - 1][j]);
                }
            }
        }
        return max[n - 1][m];
    }

    public static void main(String[] args) {
        int[] w = {1, 2, 3, 4, 5};
        int[] v = {1, 4, 2, 5, 6};
        int mw = 3;
        P01Method2 p01 = new P01Method2();
        int maxValue = p01.backPackII(mw, w, v);
        System.out.println("maxValue = " + maxValue);
    }
}
